package com.hxy.manager.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.hxy.manager.R;
import com.hxy.manager.util.VersionUtil;

/**
 *
 */
public class SettingActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
        addPreferencesFromResource(R.xml.settings);
        setTitle("设置");
        SwitchPreference useDefault = (SwitchPreference) findPreference("use_default_image");
        PreferenceScreen chekcb = (PreferenceScreen) findPreference("logs");
        chekcb.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(SettingActivity.this, LogsActivity.class));
                return false;
            }
        });
        PreferenceScreen about = (PreferenceScreen) findPreference("about");
        about.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("关于");
                //04  builder.setIcon(R.drawable.scan);
                //下一步进行主页面adapter的列表自定义。
                String versionName = VersionUtil.getVersionName(SettingActivity.this);

                builder.setMessage("版本：" + versionName + "\n作者：aohanhongzhi@qq.com\n日期：2019年2月10日");
                builder.setPositiveButton("确定", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
                builder.create();
                builder.show();
                return false;
            }
        });

        PreferenceScreen login = (PreferenceScreen) findPreference("login");
        login.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
				/*
				 //界面自定义方式
				AlertDialog.Builder ab= new AlertDialog.Builder(SettingActivity.this);
				ab.setTitle("用户登陆");
				ab.setIcon(R.drawable.login);//图片大小需要仔细研究
				LayoutInflater liInflater=(SettingActivity.this).getLayoutInflater();
				ab.setView(liInflater.inflate(R.layout.login, null));
				ab.setPositiveButton("登陆", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
				ab.setNegativeButton("取消", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
				ab.create();
				ab.show();
				*/

                //完全自定义方式
                new LoginDialog(SettingActivity.this).show();
                return false;
            }
        });


        EditTextPreference host = (EditTextPreference) findPreference("ServerUrl");
        host.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            String format = null;

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                // format = preference.getSummary().toString();
                // preference.setSummary(format.replace("{v}", newValue==null?"null":newValue.toString()));
                preference.setSummary(newValue.toString());
                return true;
            }
        });


    }
	/*
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {  
	       if ( (preference == mAirplaneModePreference) &&  
	                (Boolean.parseBoolean(  
	                    SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) ) {  
	            // In ECM mode launch ECM app dialog  
	           startActivityForResult(  
	                new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),  
	                REQUEST_CODE_EXIT_ECM);  
	            return true;  
	        }  
	        else {  
	            // Let the intents be launched by the Preference manager  
	            return false;  
	       }  
	    }
	    */


    //内部类？********************/
    public class LoginDialog extends Dialog {

        public LoginDialog(Context context) {
            super(context);
        }

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            setTitle("登陆");
            //点击查询自动弹出虚拟键盘
            EditText value = (EditText) findViewById(R.id.value);
            value.setFocusable(true);
            value.setFocusableInTouchMode(true);
            value.requestFocus();
            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

            Button find = (Button) findViewById(R.id.find);
            Button cancel = (Button) findViewById(R.id.cancel);
            find.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText value = (EditText) findViewById(R.id.value);
                    String values = value.getText().toString();

                    dismiss();
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dismiss();
                }
            });
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //  getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:  //默认添加的，向上返回上一层
                finish();
                break;
        }
        return false;
    }
}
/*
    这个写在其他的Activity
    @Override
    public void onStart()//重新启动的时候
    {
    	mSurfaceHolder = mSurfaceview.getHolder(); // 绑定SurfaceView，取得SurfaceHolder对象
    	mSurfaceHolder.addCallback(this); // SurfaceHolder加入回调接口
    	mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置显示器类型，setType必须设置
	    //读取配置文件
        SharedPreferences preParas = PreferenceManager.getDefaultSharedPreferences(SocketCameraActivity.this);
        pUsername=preParas.getString("Username", "XZY");
        serverUrl=preParas.getString("ServerUrl", "192.168.0.100");
    	String tempStr=preParas.getString("ServerPort", "8888");
    	serverPort=Integer.parseInt(tempStr);
        tempStr=preParas.getString("VideoPreRate", "1");
        VideoPreRate=Integer.parseInt(tempStr);
        tempStr=preParas.getString("VideoQuality", "85");
        VideoQuality=Integer.parseInt(tempStr);
        tempStr=preParas.getString("VideoWidthRatio", "100");
        VideoWidthRatio=Integer.parseInt(tempStr);
        tempStr=preParas.getString("VideoHeightRatio", "100");
        VideoHeightRatio=Integer.parseInt(tempStr);
        VideoWidthRatio=VideoWidthRatio/100f;
        VideoHeightRatio=VideoHeightRatio/100f;

        super.onStart();
    }

 */